// pages/tradingArea/tradingArea.js
const controller=require("../controllers/Controller").controller

Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageNumber:1,
    shopId:"",
    lastPage:false,
    type:6,
    index:0
  },

  //删除商圈活动
  delActivity(e){
    let index=e.currentTarget.dataset.index
    if(this.data.list[index].type==6){
      let value={}
      value.shopId=wx.getStorageSync('shopId')
      value.couponId=this.data.list[index].id
      controller.delCoupon(value).then(res=>{
        wx.showToast({
          title: '操作成功',
        })
      })
    }else{
      let value={}
      value.couponBagId=this.data.list[index].id
      value.shopId=this.data.shopId
      controller.delShopDelBag(value).then(res=>{
        wx.showToast({
          title: '操作成功',
        })
      })
    }
    if(this.data.type==6){
      this.getShopActivityPage()
    }else{
      this.getshopBagPage()
    }
  },


  //跳转修改
  toUpdate(e){
    let index=e.currentTarget.dataset.index
    let activityDetail=this.data.list[index]
    wx.navigateTo({
      url: '/pages/tradingArea/addCoupon?id='+activityDetail.id+"&type="+activityDetail.type,
    })
  },

  selectType(e){
    let index=e.currentTarget.dataset.type
    this.setData({
      index:index,
      pageNumber:1,
    })
    if(index===0){
      this.setData({
        type:6
      })
      this.getShopActivityPage()
    }else if(index==1){
      this.setData({
        type:2
      })
      this.getshopBagPage()
    }else if(index==2){
      this.setData({
        type:3
      })
      this.getshopBagPage()
    }else if(index==3){
      this.setData({
        type:4
      })
      this.getshopBagPage()
    }else{
      this.setData({
        type:5
      })
      this.getshopBagPage()
    }

  },

  toAddCoupon(){
    wx.navigateTo({
      url: '/pages/tradingArea/selectCoupon',
    })
  },
  toManageCoupon(){
    if(this.data.index===0){
      wx.navigateTo({
        url: '/pages/tradingArea/manageCoupon',
      })
    }else if(this.data.index===1){
      wx.navigateTo({
        url: '/pages/tradingArea/manageHotGoods?type=2',
      })
    }else if(this.data.index===2){
      wx.navigateTo({
        url: '/pages/tradingArea/manageHotGoods?type=3',
      })
    }else if(this.data.index===3){
      wx.navigateTo({
        url: '/pages/tradingArea/manageHotGoods?type=4',
      })
    }else{
      wx.navigateTo({
        url: '/pages/tradingArea/manageHotGoods?type=5',
      })
    }
  },

   //跳转优惠券详情
   toDetail(e){
    let index=e.currentTarget.dataset.index
    let id=this.data.list[index].id
    let type=e.currentTarget.dataset.type
    if(type){
      wx.navigateTo({
        url: '/pages/tradingArea/couponDetail?type='+type+'&id='+id,
      })
    }else{
      wx.navigateTo({
        url: '/pages/tradingArea/couponDetail?id='+id,
      })
    }
  },

  //店铺自己新增的优惠券列表 2.0
  getShopActivityPage(){
    let value={}
    value.pageNumber=this.data.pageNumber
    value.shopId=this.data.shopId
    value.payType=1
    value.type=this.data.type
    controller.getShopActivityPage(value).then(res=>{
      let list=res.data.list
      list.forEach(item=>{
        if(item.effectiveStartTime){
          item.effectiveStartTime=item.effectiveStartTime.split(" ")[0]
          item.effectiveEndTime=item.effectiveEndTime.split(" ")[0]
        }
        if(item.startTime){
          item.startTime=item.startTime.split(" ")[0]
          item.endTime=item.endTime.split(" ")[0]
        }
      })
      this.setData({
        list:list,
        lastPage:res.data.lastPage
      })
    })
  },
  //店铺自己新增的爆款列表 2.0
  getshopBagPage(){
    let value={}
    value.pageNumber=this.data.pageNumber
    value.shopId=this.data.shopId
    value.examineState=1
    value.type=this.data.type
    controller.getshopBagPage(value).then(res=>{
      let list=res.data.list
      list.forEach(item=>{
        if(item.timeState!=0&&item.effectiveStartTime){
          item.effectiveStartTime=item.effectiveStartTime.split(" ")[0]
          item.effectiveEndTime=item.effectiveEndTime.split(" ")[0]
        }
        if(item.startTime){
          item.startTime=item.startTime.split(" ")[0]
          item.endTime=item.endTime.split(" ")[0]
        }
      })
      this.setData({
        list:list,
        lastPage:res.data.lastPage
      })
    })
  },

  getMore(){
    if(this.data.lastPage==false){
      let pageNumber=this.data.pageNumber+1
      this.setData({
        pageNumber:pageNumber
      })
      let value={}
      value.pageNumber=this.data.pageNumber
      value.shopId=this.data.shopId
      value.payType=1
      value.type=2
      value.examineState=1
      if(this.data.type===0){
        controller.getShopActivityPage(value).then(res=>{
          let list=this.data.list
          let list2=res.data.list
          list2.forEach(item=>{
            if(item.timeState!=0&&item.effectiveStartTime){
              item.effectiveStartTime=item.effectiveStartTime.split(" ")[0]
              item.effectiveEndTime=item.effectiveEndTime.split(" ")[0]
            }
            if(item.startTime){
              item.startTime=item.startTime.split(" ")[0]
              item.endTime=item.endTime.split(" ")[0]
            }
          })
          let list3=list.concat(list2)
          this.setData({
            list:list3,
            lastPage:res.data.lastPage
          })
        })
      }else{
        controller.getshopBagPage(value).then(res=>{
          let list=this.data.list
          let list2=res.data.list
          list2.forEach(item=>{
            if(item.effectiveStartTime){
              item.effectiveStartTime=item.effectiveStartTime.split(" ")[0]
              item.effectiveEndTime=item.effectiveEndTime.split(" ")[0]
            }
            if(item.startTime){
              item.startTime=item.startTime.split(" ")[0]
              item.endTime=item.endTime.split(" ")[0]
            }
          })
          let list3=list.concat(list2)
          this.setData({
            list:list3,
            lastPage:res.data.lastPage
          })
        })
      }
    }else{

    }
  },

  onLoad: function (options) {
    this.setData({
      shopId:wx.getStorageSync('shopId')
    })
    this.getShopActivityPage()
  },

  

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getMore()
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})